package com.model.dao;

//default package
//Generated Feb 26, 2014 2:36:33 PM by Hibernate Tools 3.4.0.CR1

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 * Home object for domain model class PermissionTable.
 * 
 * @see .PermissionTable
 * @author Hibernate Tools
 */
@Stateless
public class PermissionTableDAO {

	private static final Log log = LogFactory.getLog(PermissionTableDAO.class);

	Session session;
	Transaction tx;

	public void persist(PermissionTable transientInstance) {
		log.debug("persisting PermissionTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			session.persist(transientInstance);
			tx.commit();
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void remove(PermissionTable persistentInstance) {
		log.debug("removing PermissionTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			session.delete(persistentInstance);
			tx.commit();
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public PermissionTable merge(PermissionTable detachedInstance) {
		log.debug("merging PermissionTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			PermissionTable result = (PermissionTable) session
					.merge(detachedInstance);
			tx.commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public PermissionTable findById(int id) {
		log.debug("getting PermissionTable instance with id: " + id);
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			PermissionTable instance = (PermissionTable) session.get(
					PermissionTable.class, id);
			tx.commit();
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<PermissionTable> findAll() {
		log.debug("getting PermissionTable instance ");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			// Query query =
			// session.createQuery("select distinct ot from OrderTable ot Join fetch ot.orderItemsTables oit  join fetch oit.childcatalogTable cct join fetch cct.mastercatalogTable where orderstatus <> 'Delivered'");
			Query query = session
					.createQuery(" from PermissionTable  where active= 'Y'");
			List<PermissionTable> list = query.list();
			tx.commit();
			log.debug("get successful");
			return list;
		} catch (RuntimeException re) {
			System.out.println(re);
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("get failed", re);
			throw re;
		}
	}

	public static void main(String[] args) throws Exception {
		PermissionTableDAO dao = new PermissionTableDAO();
		System.out.println(dao.findAll().size());
	}
}
